<?php


namespace App\controllers\admin;


use App\controllers\Base;
use Consts\RedisKey;
use Helper\Config;
use Helper\Output;
use Helper\MagicCrypt;
use Helper\Mysql;
use Firebase\JWT\JWT;
use Helper\Redis;
use Helper\Reg;

class Platform extends Base
{

    public function index($request, $response){

        $list = Mysql::getInstance()->get('platform');
        $conf = Mysql::getInstance()->get('system_config');
        $conf = array_column($conf,'value_','key_name');
        foreach ($list as &$item){

            $item['url'] = $conf['user_chat_url']."&p=". urlencode(MagicCrypt::getInstance()->encrypt($item['id']));
        }
        return Output::json(200,$list);

    }

    public function create($request, $response){


        $request->post['create_at'] = time();

        $res = Mysql::getInstance()->insert('platform',$request->post);
        if($res){
            Redis::getInstance()->set(RedisKey::KF_PLATFORM_LIST,json_encode(array_column(Mysql::getInstance()->where('state',1)->get('platform'),'id')));
            return Output::json(200);
        }
        return Output::json(400);
    }


    public function edit($request, $response){

        $id = $request->post['id'];
        unset($request->post['id']);
        $res = Mysql::getInstance()->where('id',$id)->update('platform',$request->post);
        if($res){
            Redis::getInstance()->set(RedisKey::KF_PLATFORM_LIST,json_encode(array_column(Mysql::getInstance()->where('state',1)->get('platform'),'id')));
            return Output::json(200);
        }
    }

}